1   /*
2    * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 6761678
27   * @summary Check properties of Annotations returned from
28   * getParameterAnnotations, including freedom from security
29   * exceptions.
30   * @run main/othervm ParameterAnnotations
31   * @author Martin Buchholz
32   */
33  
34  import java.lang.annotation.Annotation;
35  import java.lang.annotation.ElementType;
36  import java.lang.annotation.Retention;
37  import java.lang.annotation.RetentionPolicy;
38  import java.lang.annotation.Target;
39  import java.lang.reflect.Method;
40  import java.security.Permission;
41  import java.security.Policy;
42  import java.security.ProtectionDomain;
43  
44  @Retention(RetentionPolicy.RUNTIME)
45  @Target({ ElementType.FIELD, ElementType.PARAMETER })
46  @interface Named {
47    String value();
48  }
49  
50  public class ParameterAnnotations {
51  
52      // A security policy that differs from the default only in that it
53      // allows a security manager to be uninstalled.
54      static class MyPolicy extends Policy {
55          final Policy defaultPolicy;
56          MyPolicy(Policy defaultPolicy) {
57              this.defaultPolicy = defaultPolicy;
58          }
59          public boolean implies(ProtectionDomain pd, Permission p) {
60              return p.getName().equals("setSecurityManager") ||
61                  defaultPolicy.implies(pd, p);
62          }
63      }
64  
65      public void nop(@Named("foo") Object foo,
66                      @Named("bar") Object bar) {
67      }
68  
69      void test(String[] args) throws Throwable {
70          // Test without a security manager
71          test1();
72  
73          // Test with a security manager
74          Policy defaultPolicy = Policy.getPolicy();
75          Policy.setPolicy(new MyPolicy(defaultPolicy));
76          System.setSecurityManager(new SecurityManager());
77          try {
78              test1();
79          } finally {
80              System.setSecurityManager(null);
81              Policy.setPolicy(defaultPolicy);
82          }
83      }
84  
85      void test1() throws Throwable {
86          for (Method m : thisClass.getMethods()) {
87              if (m.getName().equals("nop")) {
88                  Annotation[][] ann = m.getParameterAnnotations();
89                  equal(ann.length, 2);
90                  Annotation foo = ann[0][0];
91                  Annotation bar = ann[1][0];
92                  equal(foo.toString(), "@Named(value=foo)");
93                  equal(bar.toString(), "@Named(value=bar)");
94                  check(foo.equals(foo));
95                  check(! foo.equals(bar));
96              }
97          }
98      }
99  
100     //--------------------- Infrastructure ---------------------------
101     volatile int passed = 0, failed = 0;
102     void pass() {passed++;}
103     void fail() {failed++; Thread.dumpStack();}
104     void fail(String msg) {System.err.println(msg); fail();}
105     void unexpected(Throwable t) {failed++; t.printStackTrace();}
106     void check(boolean cond) {if (cond) pass(); else fail();}
107     void equal(Object x, Object y) {
108         if (x == null ? y == null : x.equals(y)) pass();
109         else fail(x + " not equal to " + y);}
110     static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass();
111     public static void main(String[] args) throws Throwable {
112         try {thisClass.getMethod("instanceMain",String[].class)
113                 .invoke(thisClass.newInstance(), (Object) args);}
114         catch (Throwable e) {throw e.getCause();}}
115     public void instanceMain(String[] args) throws Throwable {
116         try {test(args);} catch (Throwable t) {unexpected(t);}
117         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
118         if (failed > 0) throw new AssertionError("Some tests failed");}
119 }